AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	self.Inputs = CDE_Wire_CreateInputs(self.Entity, { "Fire" })
	self.Outputs = CDE_Wire_CreateOutputs(self.Entity, { "Can Fire" , "Cooldown Time" })
	
	CDE_RD_AddResource(self, "energy", 0)
	self.Entity:NextThink(CurTime())
	
	self.PrintName = self.DamageType .. " " .. self.PrintName
	self.NextReady = CurTime()
	self.Entity:SetNetworkedString( "GModOverlayText", self.PrintName )
	self.WireDebugName = self.PrintName
end

function ENT:TriggerInput(k,v)
	if (k == "Fire") then
		self.Firing = util.tobool(v)
	end
end

function ENT:Think()
	--self.BaseClass.Think(self)
	if self.Firing and (CurTime() >= self.NextReady) and (not self.Entity.CDamage.cloaked) and (CDE_RD_GetResourceAmount(self.Entity, "energy", true) >= self.EnergyDraw) then
		self.Entity:FireShot(self.ProjectileName)
		CDE_RD_ConsumeResource(self.Entity, "energy", self.EnergyDraw, true)
		self.NextReady = CurTime() + self.Cooldown
	end
	local canfire
	if (CurTime() >= self.NextReady) and (self.Entity.CDamage and (not self.Entity.CDamage.cloaked)) then canfire = 1 else canfire = 0 end
	CDE_Wire_TriggerOutput(self.Entity, "Can Fire", canfire)
	CDE_Wire_TriggerOutput(self.Entity, "Cooldown Time", math.Clamp(self.NextReady - CurTime(), 0, self.Cooldown))
	self.Entity:NextThink(CurTime() + 0.25)
	return true
end

function ENT:FireShot(entitytype)
	local shot = ents.Create(entitytype)
	shot:SetPos(self.Entity:LocalToWorld(self.Entity:OBBCenter()))
	shot:SetAngles(self.Entity:FireDirection())
	shot:SetPlayer(self.Entity:GetPlayer())
	shot:SetOwner(self.Entity)
	shot.Damage = self.Entity.Damage
	shot.DamageType = self.DamageType
	shot.BlastRadius = self.BlastRadius
	shot.RenderMode = self.RenderMode
	shot.Speed = self.ProjectileSpeed
	shot.Color = self.Color
	shot:Spawn()
	shot:Activate()
	shot:GetPhysicsObject():Wake()
	shot:GetPhysicsObject():SetVelocity(self.Entity:FireDirection() * self.ProjectileSpeed)
	shot:SetAngles(self.Entity:FireDirection())
	self.Entity:DrawFireEffect()
end

function ENT:Touch( ent )
	if ent.HasHardpoints then
		if ent.Cont && ent.Cont:IsValid() then HPLink( ent.Cont, ent.Entity, self.Entity ) end
	end
end